/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.libraries.rabbitmq.impl.publisher;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.MessageProperties;
import it.txt.access.capability.libraries.rabbitmq.AbstractApplication;
import it.txt.access.capability.libraries.rabbitmq.exception.AbstractApplicationException;
import it.txt.access.capability.libraries.rabbitmq.settings.AbstractApplicationSettings;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

/**
 *
 * @author seccia
 */
public class PublisherApplication extends AbstractApplication {

    public static final String PUBLISHER_PATTERN_SEPARATOR = "Pattern=";
    public static final String CHANNEL_PUB_PROPS_TIME_STAMP_LONG = "TIME_STAMP_LONG";
    public static final String CHANNEL_PUB_PROPS_ENTITY_DATA_URI = "ENTITY_DATA_URI";
    public static final String CHANNEL_PUB_PROPS_ENTITY_URI = "ENTITY_URI";
    public static final String CHANNEL_PUB_PROPS_ID = "SUBJECT_ID";
    private AMQP.BasicProperties amqpProps;

    public PublisherApplication(AbstractApplicationSettings settings) throws AbstractApplicationException {
        super(settings);
    }
    
    @Override
    public void sessionRenegotiation() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void basicPublish(String messageBody) throws AbstractApplicationException {
        LOGGER.log(Level.INFO, "Publishing message: {0}", messageBody);
        try {
            getCapabilityConnection().getChannel().basicPublish(
                    // Eschenage ad the element returned by the response
                    getCapabilityConnection().getQueueName(),
                    // Routing Key as the pattern
                    ((PublisherAppModel)applicationModel).getLeafNodeID().split(PUBLISHER_PATTERN_SEPARATOR)[1],
                    // the properties
                    getBasicProperties(),
                    // the message bytes
                    messageBody.getBytes());
            LOGGER.log(Level.INFO, "Message published.");
        }
        catch (IOException ex) {
            throw new AbstractApplicationException(ex.getMessage(), ex);
        }
    }

    private AMQP.BasicProperties getBasicProperties(){
        if(amqpProps == null){
            amqpProps = MessageProperties.BASIC;
            Map<String, Object> headers = new HashMap<String, Object>();
            headers.put(CHANNEL_PUB_PROPS_ID, 
                    ((PublisherAppModel)applicationModel).getSignerInfo().getSignerID());
            headers.put(CHANNEL_PUB_PROPS_TIME_STAMP_LONG,
                    new Date().getTime());
            headers.put(CHANNEL_PUB_PROPS_ENTITY_DATA_URI, 
                    ((PublisherAppModel)applicationModel).getEntityDataURI());
            headers.put(CHANNEL_PUB_PROPS_ENTITY_URI, 
                    ((PublisherAppModel)applicationModel).getEntityURI());
        }
        return amqpProps;
    }
}
